﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace GenerateReports
{
    public partial class Util
    {
        public static void WriteToFile(List<Dictionary<string, string>> Result, string Path)
        {
            // generate csv file
            using (FileStream fs = File.Create(Path))
            {
                string text;
                byte[] info;

                string[] keys = Result[0].Keys.ToArray();
                text = String.Join(",", keys);

                foreach (var data in Result)
                {
                    text += "\r\n";
                    for (var i = 0; i < data.Count; i++)
                    {
                        text += data[keys[i]] + ",";
                    }
                }

                info = new UTF8Encoding(true).GetBytes(text);
                fs.Write(info, 0, info.Length);
            }
        }
    }
}
